// pages/student/index/index.js
var util = require('../../../utils/util.js')

const ald = require('../../../utils/ald-stat.js');

//小神推
const xst_app = require('../../../utils/pushsdk.js').pushSdk(App, 'App').App;
const xst_page = require('../../../utils/pushsdk.js').pushSdk(Page).Page;

var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabBarData: {
      list: app.tabBar,
      current: 0 ,
    },
    phone:'',
    getContactChange: false,
    getPhoneNumberChange: false,
    getUserChange: false,
    is_first:'0',
    id_number:'',
    relation_name:'游客'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let isIphoneX = app.globalData.isIphoneX;
    this.setData({
      id_number: options.userID ? options.userID : '有问题二维码' 
    })

    //判断是否是扫码进来的
    var phone = wx.getStorageSync('phone') ;
    console.log(options)
    if (options.userID !== undefined) { 

      if (options.userID == "GK23808"){ //临时判断后面去掉
        wx.setStorageSync('userID', 155);
        wx.setStorageSync('userNumber', options.userID); //临时ID
      }else{
        wx.setStorageSync('userID', options.userID);
        wx.setStorageSync('userNumber', options.userNumber); //临时ID
      }
      
      //扫码进来的
      this.setData({
        getContactChange: true
      });
    }
  },
  /**
   * 关闭签到
   */
  btnClosed(){
    this.setData({
      getContactChange: false
    });
  },

  /**
   * 签到
   */
  btnCheck(){
    if (!wx.getStorageSync('phone')) {
      wx.login({
        success: res => {
          wx.setStorageSync('code', res.code);
          this.setData({
            getPhoneNumberChange: true
          });
        }
      }) 
    }else{
      wx.showLoading({
        title: '拼命加载中...',
      });
      //有电话号码  
      var phone = wx.getStorageSync('phone'),
        wx_name = wx.getStorageSync('wx_name'),
        time_data = (Date.parse(new Date())) / 1000,
        string_data = '',
        csData = {};
      string_data = 'id_number=' + this.data.id_number + '&learn_center_id=8038&relation_name=' + this.data.relation_name+'&relation_phone=' + phone + '&appsecret=VHRNwY1IfNE8YljsbqMnYuubuuTvlisJ&time=' + time_data;
      console.log(string_data) ;
      var sign_data = util.sha1(string_data);
      csData = {
        'appid': 'YX_1iyn1224on',
        'sign': sign_data,
        'time': time_data,
        'learn_center_id': '8038',
        'id_number': this.data.id_number,
        'relation_name': this.data.relation_name,
        'relation_phone': phone

      }
      wx.request({
        url: 'https://api.emp.eenet.com/preparerelationapi/addRelation.html',
        data: csData,
        success: (res) => {
          res = res.data;
          if (res.status == 1) {
            wx.showToast({
              title: '签到成功',
              icon: 'none'
            });
          } else {
            wx.showToast({
              title: '签到失败',
              icon: 'none'
            });
          }
          this.setData({
            getContactChange: false
          });
        }
      })
    }
    
  },


  /**
 * 解析手机号码/unionID
 */
  parsePhone(encryptedData, iv) {
    wx.showLoading({
      title: '拼命加载中...',
    });
    wx.request({
      url: app.globalData.url + 'c=home&m=getPhoneNumber',
      data: {
        'encryptedData': encryptedData,
        'iv': iv,
        'code': wx.getStorageSync('code')
      },
      success: (res) => {
        res = res.data;
        if (res.status == 200) {
          //拿到电话号码
          this.setData({
            'phone': res.data.purePhoneNumber
          })
          wx.setStorageSync('phone', res.data.purePhoneNumber);
          wx.hideLoading();
          this.btnCheck() ;//
        } else {
          wx.showToast({
            title: '获取个人信息失败，请重试',
            icon: 'none'
          })
          this.setData({
            getPhoneNumberChange: true
          })
        }

      }
    })
  },

   //手机号码授权回调
  getPhoneNumber(e) {
    if (e.detail.iv && e.detail.encryptedData) {
      this.parsePhone(e.detail.encryptedData, e.detail.iv);
      this.setData({
        getPhoneNumberChange: false
      })
    } else {
      wx.showToast({
        title: '授权失败',
        icon: 'none'
      })
      this.setData({
        getPhoneNumberChange: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})